﻿ using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Net;
using BMC.ARSystem;
using System.IO;
using BMC.ARSystem.Utilities.Common;
using System.Collections.ObjectModel;
using GCPUD.Autoprov.Network;

namespace GCPUD.Autoprov.RemedyLib
{
    public class RemedyServer
    {
        private Server srv = new Server();
        public RemedyServer(String ipaddress, int port, String user, String password)
        {
            try
            {

                srv.Login(ipaddress, user, password);
                srv.SetServerPort(port, 0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
        ~RemedyServer()
        {
            srv.Logout();
        }
        public Server ARServer
        {
            get
            {
                if (srv != null && srv.Version != null)
                {
                    return srv;
                }
                else return null;
            }
        }
            
    }

    public class Remedy
    {

        public static List<GatewayAccountEntry> remedyGatewayAccount(RemedyServer server, string qual)
        {
            Server srv = server.ARServer;
            List<GatewayAccountEntry> list = new List<GatewayAccountEntry>();
            ArrayList listGateWayAccount = new ArrayList();
            try
            {
                listGateWayAccount = srv.GetListEntry("ZIPP:CXGatewayAccount_ga", qual);

                foreach (EntryDescription entryDescription in listGateWayAccount)
                {
                    GatewayAccountEntry gaEntry = Remedy.GatewayAccount(server, entryDescription.EntryId);
                    if (gaEntry.RequestID != null)
                    {
                        list.Add(gaEntry);
                    }

                }

            }
            catch (ARException ex)
            {
                Console.WriteLine("Remedy Error while getting GatewayAccount info: " + ex.Message);
                return null;
            }
            return list;
        }
        public static GatewayAccountEntry GatewayAccount(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm remedyGatewayAccount = new RemedyForm(srv.GetEntry("ZIPP:CXGatewayAccount_ga", requestID));

                    return new GatewayAccountEntry(
                        remedyGatewayAccount.FormData[GatewayAccountFields.requestId_field],
                        remedyGatewayAccount.FormData[GatewayAccountFields.gatewayId_field],
                        remedyGatewayAccount.FormData[GatewayAccountFields.accountId_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }
            else return null;
        }
        public static List<ProvisioningEntry> RemedyProvisionings(RemedyServer server, string qual)
        {
            Server srv = server.ARServer;
            List<ProvisioningEntry> list = new List<ProvisioningEntry>();
            ArrayList listProvs = new ArrayList();
            try
            {
                //string qual = "'De-provisioning Date' = $NULL$ AND 'Provisioning Date' != $NULL$";
                //Console.WriteLine("qual = " + qual);
                listProvs = srv.GetListEntry("ZIPP:Provisioning", qual);

                foreach (EntryDescription entryDescription in listProvs)
                {
                    ProvisioningEntry provisioning = Provisioning(server, entryDescription.EntryId);
                    if (provisioning.RequestID != null)
                    {
                        list.Add(provisioning);
                    }
                }
            }
            catch (ARException ex)
            {
                Console.WriteLine("Remedy error while getting provisioning entries: " + ex.Message);
            }
            return list;
        }
        public static ProvisioningEntry Provisioning(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm remedyProvisioning = new RemedyForm(srv.GetEntry("ZIPP:Provisioning", requestID));

                    return new ProvisioningEntry(
                        remedyProvisioning.FormData[ProvisioningDataFields.requestId_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.gen_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.stencil],
                        remedyProvisioning.FormData[ProvisioningDataFields.accountId_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.gatewayId_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.portNum_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.portSlot_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.vlanNum_field],
                        remedyProvisioning.FormData[ProvisioningDataFields.tag_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }
            else return null;

        }
        public static QueueEntry Queue(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm remedyQueue = new RemedyForm(srv.GetEntry("ZIPP:Queue", requestID));

                    return new QueueEntry(
                        remedyQueue.FormData[QueueDataFields.requestId_field],
                        remedyQueue.FormData[QueueDataFields.stencilField],
                        remedyQueue.FormData[QueueDataFields.gatewayIpField],
                        remedyQueue.FormData[QueueDataFields.commandField],
                        remedyQueue.FormData[QueueDataFields.scheduleField],
                        remedyQueue.FormData[QueueDataFields.resultField]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine(ex);
                    return null;
                }
            }
            else return null;
        }
        public static List<GatewayEntry> RemedyGateways(RemedyServer server, string qual)
        {
            Server srv = server.ARServer;
            //Console.WriteLine("RemedyGateways Start");
            List<GatewayEntry> list = new List<GatewayEntry>();
            ArrayList listGateways = new ArrayList();
            
            try
            {
                
                listGateways = srv.GetListEntry("ZIPP:Gateway", qual);
            }
            catch (ARException ex)
            {
                Console.WriteLine("Houston... We have a problem in GetActiveGatewayIPAddress function: ", ex);
            }

            foreach (EntryDescription entryDescription in listGateways)
            {
                GatewayEntry gateway = Remedy.Gateway(server, entryDescription.EntryId);
                if (gateway.RequestID != null)
                {
                    list.Add(Remedy.Gateway(server, entryDescription.EntryId));
                }
            }

            return list;
        }
        public static GatewayEntry Gateway(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm remedyGateway = new RemedyForm(srv.GetEntry("ZIPP:Gateway", requestID));
                    return new GatewayEntry(
                        remedyGateway.FormData[GatewayDataFields.requestId_field],
                        remedyGateway.FormData[GatewayDataFields.gatewayIP_field],
                        remedyGateway.FormData[GatewayDataFields.gatewayMac_field],
                        remedyGateway.FormData[GatewayDataFields.stencilNum_field],
                        remedyGateway.FormData[GatewayDataFields.model_field],
                        remedyGateway.FormData[GatewayDataFields.partNumber_field],
                        remedyGateway.FormData[GatewayDataFields.igmpSnooping_field],
                        remedyGateway.FormData[GatewayDataFields.dnsName_field],
                        remedyGateway.FormData[GatewayDataFields.codeVersion_field],
                        remedyGateway.FormData[GatewayDataFields.versionNumber_field],
                        remedyGateway.FormData[GatewayDataFields.dhcpDisabled_field],
                        remedyGateway.FormData[GatewayDataFields.dateInstalled_field],
                        remedyGateway.FormData[GatewayDataFields.dateRemoved_field],
                        remedyGateway.FormData[GatewayDataFields.surplus_field]);
                }
                catch (ARException e)
                {
                    Console.WriteLine("Error Getting gateway entry: " + requestID + " : " + e);
                    return null;
                }
            }
            else return null;
        }
        public static List<SplicingEntry> RemedySplicing(RemedyServer server, string qual)
        {
            Server srv = server.ARServer;
            //Console.WriteLine("RemedySplicing Start");
            List<SplicingEntry> list = new List<SplicingEntry>();
            ArrayList listSplice = new ArrayList();

            try
            {
                //string qual = "'Stencil #' != $NULL$ AND 'Switch Blade' != $NULL$ AND 'Switch Port' != $NULL$";
                listSplice = srv.GetListEntry("ZIPP:Splice", qual);
            }
            catch (ARException ex)
            {
                Console.WriteLine("Houston... We have a problem in RemedySplicing function: ", ex);
            }
            int tempCounter = listSplice.Count;
            foreach (EntryDescription entryDescription in listSplice)
            {
                SplicingEntry splice = Remedy.Splice(server, entryDescription.EntryId);
                if (!(splice.RequestID == null))
                {
                    list.Add(splice);
                }
            }
            return list;
        }
        public static SplicingEntry Splice(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm remedySplice = new RemedyForm(srv.GetEntry("ZIPP:Splice", requestID));
                    //public SplicingEntry(string remedyID, string switchPort, string switchBlade, string hubNumber, string switchIPAddress, string premise)
                    return new SplicingEntry(
                        remedySplice.FormData[SplicingDataFields.requestId_field],
                        remedySplice.FormData[SplicingDataFields.stencil_field],
                        remedySplice.FormData[SplicingDataFields.switchPort_field],
                        remedySplice.FormData[SplicingDataFields.switchBlade_field],
                        remedySplice.FormData[SplicingDataFields.hubNumber_field],
                        remedySplice.FormData[SplicingDataFields.switchIpAddress_field],
                        remedySplice.FormData[SplicingDataFields.premise_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine("Houston... We have a problem in RemedySplicing function: ", ex);
                    return null;
                }
            }
            else return null;

        }
        public static List<HubEntry> RemedyHubList(RemedyServer server)
        {
            Server srv = server.ARServer;
            //Console.WriteLine("RemedyHubList Start");
            List<HubEntry> hubs = new List<HubEntry>();
            ArrayList listHubs = new ArrayList();
            string qualHubs = "'Switch IP Address' != $NULL$";
            listHubs = srv.GetListEntry("ZIPP:Hub", qualHubs);

            foreach (EntryDescription entryDescription in listHubs)
            {
                HubEntry hub = Remedy.Hub(server, entryDescription.EntryId);
                if (hub.RequestID != null)
                {
                    hubs.Add(hub);
                }
            }
            return hubs;
        }
        public static HubEntry Hub(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm hubForm = new RemedyForm(srv.GetEntry("ZIPP:Hub", requestID));
                    return new HubEntry(
                        hubForm.FormData[HubDataFields.requestId_field],
                        hubForm.FormData[HubDataFields.hubdescription_field],
                        hubForm.FormData[HubDataFields.switchIpAddress_field],
                        hubForm.FormData[HubDataFields.scope_field],
                        hubForm.FormData[HubDataFields.facility_field],
                        hubForm.FormData[HubDataFields.hubLocation_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine("Houston... We have a problem in Hub function: ", ex);
                    return null;
                }
            }
            else return null;

        }
        public static List<AccountEntry> RemedyAccountList(RemedyServer server, string qualAccounts)
        {
            Server srv = server.ARServer;
            //Console.WriteLine("RemedyAccountList Start");
            List<AccountEntry> accounts = new List<AccountEntry>();
            ArrayList listAccounts = new ArrayList();

            listAccounts = srv.GetListEntry("ZIPP:Accounts", qualAccounts);

            foreach (EntryDescription entryDescription in listAccounts)
            {
                AccountEntry account = Remedy.Account(server, entryDescription.EntryId);
                if (account.RequestID != null)
                {
                    listAccounts.Add(account);
                }
            }
            return accounts;
        }
        public static AccountEntry Account(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm accountForm = new RemedyForm(srv.GetEntry("ZIPP:Account", requestID));
                    return new AccountEntry(
                        accountForm.FormData[AccountDataFields.requestId_field],
                        accountForm.FormData[AccountDataFields.accountName_field],
                        accountForm.FormData[AccountDataFields.accountAddress_field],
                        accountForm.FormData[AccountDataFields.premise_field],
                        accountForm.FormData[AccountDataFields.primaryHubId_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine("Houston... We have a problem in Account function: ", ex);
                    return null;
                }
            }
            else return null;
        }
        public static List<VlanEntry> RemedyVlans(RemedyServer server)
        {
            Server srv = server.ARServer;
            //Console.WriteLine("RemedyVlan Start");
            List<VlanEntry> vlans = new List<VlanEntry>();
            ArrayList listVlans = new ArrayList();
            string qual = "'Out of Service' != $NULL$";
            listVlans = srv.GetListEntry("ZIPP:VLAN", qual);
            foreach (EntryDescription entryDescription in listVlans)
            {
                VlanEntry vlan = Remedy.Vlan(server, entryDescription.EntryId);
                if (vlan.RequestID != null)
                {
                    listVlans.Add(vlan);
                }
            }
            return vlans;
        }
        public static VlanEntry Vlan(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            if (requestID != null)
            {
                try
                {
                    RemedyForm accountForm = new RemedyForm(srv.GetEntry("ZIPP:VLAN", requestID));
                    return new VlanEntry(
                        accountForm.FormData[VlanDataFields.requestId_field],
                        accountForm.FormData[VlanDataFields.vlanNum_field],
                        accountForm.FormData[VlanDataFields.vlanName_field],
                        accountForm.FormData[VlanDataFields.isolated_field],
                        accountForm.FormData[VlanDataFields.primaryVlan_field]);
                }
                catch (ARException ex)
                {
                    Console.WriteLine("Houston... We have a problem in Vlan function: ", ex);
                    return null;
                }
            }
            else return null;
        }
        public static void SetSpliceBladePort(RemedyServer server, string requestID, string blade, string port)
        {
            Server srv = server.ARServer;
            FieldValueList fvl = new FieldValueList();
            fvl[SplicingDataFields.switchBlade_field] = blade;
            fvl[SplicingDataFields.switchPort_field] = port;

            try
            {
                srv.SetEntry("ZIPP:Splice", requestID, fvl);
                Console.WriteLine("Set the blade and port for requestid: " + requestID + " to:" + blade + "/" + port);
            }
            catch (ARException ex)
            {
                Console.WriteLine("An AR Exception occured this trying to clear the gen field: " + ex.Message);
            }
        }
        public static string CreateGatewayQueue(RemedyServer server, string stencil, string gatewayIP, string result)
        {
            Server srv = server.ARServer;
            FieldValueList fvl = new FieldValueList();
            fvl[QueueDataFields.stencilField] = stencil;
            fvl[QueueDataFields.gatewayIpField] = gatewayIP;
            fvl[QueueDataFields.commandField] = "Hub_Prov";
            fvl[QueueDataFields.scheduleField] = DateTime.Now.ToString();
            fvl[QueueDataFields.resultField] = result;
            string requestID = null;
            try
            {
                requestID = srv.CreateEntry("ZIPP:Queue", fvl);
            }
            catch (ARException ex)
            {
                Console.WriteLine("AR Exception: " + ex.Message);
                return requestID;
            }
            return requestID;
        }
        public static void UpdateGatewayQueue(RemedyServer server, string requestID, string schedule, string result)
        {
            Server srv = server.ARServer;
            FieldValueList fvl = new FieldValueList();
            fvl[QueueDataFields.scheduleField] = DateTime.Now.ToString();
            fvl[QueueDataFields.resultField] = result;
            try
            {
                srv.SetEntry("ZIPP:Queue", requestID, fvl);
            }
            catch (ARException ex)
            {
                Console.WriteLine("AR Exception: " + ex.Message);
            }
        }
        public static void ClearGatewayFromSplice(RemedyServer server, string requestID)
        {
            FieldValueList fvl = new FieldValueList();
            fvl[SplicingDataFields.stencil_field] = "";


            try
            {
                //srv.SetEntry("ZIPP:Splice", requestID, fvl);
                Console.WriteLine("Cleared the Gateway from Splice: " + requestID);
            }
            catch (ARException ex)
            {
                Console.WriteLine("An AR Exception occured this trying to clear the gen field: " + ex.Message);
            }
        }
        public static void ClearProvisioningGenField(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            requestID = "ZPROV0000070447"; //let not break everything
            FieldValueList fvl = new FieldValueList();
            fvl[ProvisioningDataFields.gen_field] = "";
            try
            {
                srv.SetEntry("ZIPP:Provisioning", requestID, fvl);
                Console.WriteLine("Cleared the gen field for requestid: " + requestID);
            }
            catch (ARException ex)
            {
                Console.WriteLine("An AR Exception occured this trying to clear the gen field: " + ex.Message);
            }
        }

        internal void UpdateProvisioningGenField(RemedyServer server, string remedyid)
        {
            Server srv = server.ARServer;
            if (remedyid == "ZPROV0000070447")
            {
                Remedy.ClearProvisioningGenField(server, remedyid);
            }
            //let not break everything
            //to be implemented
            //for now just write something on the console
            Console.WriteLine("Just gonna update if the ProvID = 70447");
            //throw new NotImplementedException();
        }
        private bool isValidProvisioning(RemedyServer server, string requestID)
        {
            Server srv = server.ARServer;
            // get the provisioning data
            ProvisioningEntry provisioning = Remedy.Provisioning(server, requestID); //get the provisioning request

            Console.WriteLine(provisioning.RequestID);
            Console.WriteLine(provisioning.GatewayID);
            Console.WriteLine(provisioning.AccountID);

            // Validate that the gateway record exists
            GatewayEntry gateway = Remedy.Gateway(server, provisioning.GatewayID);

            if (gateway.RequestID == null)
            {
                Console.WriteLine(provisioning.GatewayID + " was not found");
                return false;
            }

            // Validate that the account record exists
            AccountEntry account = Remedy.Account(server, provisioning.AccountID);
            if (account.RequestID == null)
            {
                Console.WriteLine(provisioning.AccountID + " was not found");
                return false;
            }

            // Validate that there is a valid splice request
            List<SplicingEntry> spliceEntries = Remedy.RemedySplicing(server, "'Stencil #' != \"" + gateway.StencilNum + "\"");


            if (spliceEntries.Count < 1)
            {
                return false;
            }

            //
            return true;
        }
        public static List<ProvisioningEntry> GetNewProvisioningRequests(RemedyServer server)
        {
            List<ProvisioningEntry> returnProvisioningRequestIds = new List<ProvisioningEntry>();
            //ArrayList newProvReqs = new ArrayList();
            try
            {
                List<string> gwids = new List<string>();
                //Constants.AR_COM_PARM_NULL
                string qual = "'gen' = \"gxupdate\" AND 'Provisioning Date' != $NULL$ AND 'De-provisioning Date' = $NULL$"; //536870957 is gen field and 536870932 is vlan field
                List<ProvisioningEntry> entries = Remedy.RemedyProvisionings(server, qual);

                foreach (ProvisioningEntry entry in entries)
                {

                    returnProvisioningRequestIds.Add(entry);

                }
                qual = "'gen' = \"gxupdate\" AND 'Provisioning Date' != $NULL$ AND 'De-provisioning Date' < \"" 
                    + DateTime.Now 
                    + "\""; //536870957 is gen field and 536870932 is vlan field
                //Console.WriteLine(DateTime.Now + ":" + qual);
                entries = Remedy.RemedyProvisionings(server, qual);
                foreach (ProvisioningEntry entry in entries)
                {

                    returnProvisioningRequestIds.Add(entry);

                }
            }
            catch (ARException ex)
            {
                Console.WriteLine("While getting new provisioning requests: " + ex.Message);
            }

            return returnProvisioningRequestIds;
        }
        public static ArrayList GetVuis(RemedyServer server, string form)
        {
            Server srv = server.ARServer;
            ArrayList vuis = new ArrayList();
            ArrayList vuiIds = srv.GetListVui(form);
            foreach (uint vuiId in vuiIds)
            {
                Vui vui = srv.GetVui(form, (uint)vuiId);
                vuis.Add(vui);
            }
            return vuis;
        }
        private static Hashtable GetAllFields(RemedyServer server, string form)
        {
            Hashtable fields;
            try
            {
                fields = server.ARServer.GetAllFields(form);
            }
            catch (ARException ex)
            {
                Console.WriteLine("Remedy Error:"+ ex);
                fields = null;
            }
            return fields;
        }
        public static List<string> GetFormFields(RemedyServer server,string formName)
        {

            List<string> returnFormFieldList = new List<string>();
            Hashtable allFields = Remedy.GetAllFields(server, formName);
            if (!(allFields == null))
            {
                foreach (DictionaryEntry de in allFields)
                {
                    Field field = de.Value as Field;
                    returnFormFieldList.Add(de.Key + ":" + field.Name+ ":" + field.GetType());


                }
                return returnFormFieldList;
            }
            else return null;
        }


    }
    public class RemedyField
    {
        public int FieldID { get; set; }
        public Field Field { get; set; }
    }
    public class QueueDataFields
    {
        public const int
            requestId_field = 1,
            stencilField = 536870914,
            gatewayIpField = 536870915,
            commandField = 536870916,
            resultField = 536870918,
            scheduleField = 536870917;
    }
    public static class HubDataFields
    {
        public const int
            facility_field = 536870921,
            requestId_field = 1,
            hubdescription_field = 536870920,
            switchIpAddress_field = 536870919,
            hubLocation_field = 536870918,
            scope_field = 536870927;

    }
    public static class GatewayDataFields
    {
        public const int
            warranty_field = 536871061,
            autoprovision_field = 536871060,
            validate_field = 536871059,
            remedy_field = 536871058,
            gatewayDetail_field = 536871057,
            gateway_field = 536871056,
            gatewayIP_field = 536871054,
            gatewayMac_field = 536871050,
            gatewaySerial_field = 536871049,
            stencilNum_field = 536871036,
            dateRemoved_field = 536871028,
            dateInstalled_field = 536871026,
            warrantyEnd_field = 536870954,
            warrantyBegin_field = 536870953,
            accountName_field = 536870937,
            dhcpDisabled_field = 536870941,
            address_field = 536870940,
            model_field = 536870939,
            flag_provisioning_date_field = 536870928,
            flag_provisioning_field = 536870927,
            igmpSnooping_field = 536870935,
            dnsName_field = 536870934,
            surplus_field = 536870933,
            manufacturer_field = 536870931,
            partNumber_field = 536870930,
            codeVersion_field = 536870929,
            versionNumber_field = 536870917,
            requestId_field = 1;
        /*
            Code Version	Code Version	536870929
            flag_verification	flag_verification	536870918
            version_number	version_number	536870917
            New Install	New Install	536870916
            Hub Interface Management History	Hub Interface Management History	536870925
            Hub Interface Management	Hub Interface Management	536870924
            flag_remove_date	flag_remove_date	536870923
            flag_remove	flag_remove	536870922
            flag_reboot_date	flag_reboot_date	536870921
            flag_reboot	flag_reboot	536870920
            flag_verification_date	flag_verification_date	536870919
            Gateway Version	Gateway Version	536870909
            Clock_btn		536870908
            Status History	(not on view)	15
            Hardware	Hardware	536870915
            tmp_Stencil #	tmp_Stencil #	536870914
            Item Type	Item Type	536870913
            Reboot Gateway	Reboot Gateway	536870910
            Short Description	Short Description	8
            Status	Status	7
            Modified Date	Modified Date	6
            Last Modified By	Last Modified By	5
            Assigned To	Assigned To	4
            Create Date	Create Date	3
            Submitter	Submitter	2
            Request ID	Request ID	1
        */
    }
    public static class AccountDataFields
    {
        public const int requestId_field = 1,
            accountName_field = 536870913,
            accountAddress_field = 536870915,
            premise_field = 536870970,
            primaryHubId_field = 536870921;
    }
    public static class ProvisioningDataFields
    {
        public const int requestId_field = 1,
            gen_field = 536870957,
            stencil = 536870948,
            accountId_field = 536870931,
            gatewayId_field = 536870937,
            portNum_field = 536870913,
            portSlot_field = 536870973,
            vlanNum_field = 536870932,
            tag_field = 536870927;
    }
    public static class VlanDataFields
    {
        public const int requestId_field = 1,
            vlanNum_field = 536870923,
            vlanName_field = 536870920,
            isolated_field = 536870921,
            primaryVlan_field = 536870919;
    }
    public static class SplicingDataFields
    {
        public const int requestId_field = 1,
        switchPort_field = 536870932,
        switchBlade_field = 536870936,
        hubNumber_field = 536870933,
        switchIpAddress_field = 536870918,
        premise_field = 536870956,
        stencil_field = 536870914;



    }
    public static class GatewayAccountFields
    {
        public const int requestId_field = 1,
            gatewayId_field = 536870930,
            accountId_field = 536870928;
    }


    /// <summary>
    /// RemedyGateway Class stores data from Remedy into an object
    /// </summary>
    /// 
    public class RemedyForm
    {
        /// <summary>
        /// Stores the Form data from Remedy in the RemedyForm object
        /// </summary>
        public Dictionary<int, string> FormData;

        public RemedyForm(FieldValueList _data)
        {
            if (_data != null)
            {
                FormData = new Dictionary<int, string>();
                foreach (uint field in _data.Fields)
                {
                    FormData.Add((int)field, _data[field].ToString());
                }
            }
        }
    }
    public class ProvisioningEntry
    {
        public ProvisioningEntry(string requestID, string gen, string stencil, string accountID, string gatewayID, string portNum, string slotNum, string vlanNum, string tag)
        {
            RequestID = requestID;
            Gen = gen;
            Stencil = stencil;
            AccountID = accountID;
            GatewayID = gatewayID;
            PortNum = portNum;
            SlotNum = slotNum;
            VlanNum = vlanNum;
            Tag = tag;
        }

        public string RequestID;
        public string Gen;
        public string Stencil;
        public string AccountID;
        public string GatewayID;
        public string PortNum;
        public string SlotNum;
        public string VlanNum;
        public string Tag;

        public override string ToString()
        {
            return RequestID;
        }

    }
    public class GatewayEntry
    {
        public GatewayEntry(string requestID, string gatewayIP, string gatewayMAC, string stencilNum, string model, string partNumber, string iGMPSnooping, string dNSName, string codeVersion, string versionNumber, string dHCPDisabled, string dateInstalled, string dateRemoved, string surplus)
        {
            RequestID = requestID;
            GatewayIP = gatewayIP;
            GatewayMAC = new Mac(gatewayMAC);
            StencilNum = stencilNum;
            Model = model;
            PartNumber = partNumber;
            IGMPSnooping = iGMPSnooping;
            DNSName = dNSName;
            CodeVersion = codeVersion;
            VersionNumber = versionNumber;
            DHCPDisabled = dHCPDisabled;
            DateInstalled = dateInstalled;
            DateRemoved = dateRemoved;
            Surplus = surplus;
        }


        public string RequestID;
        public string GatewayIP;
        public Mac GatewayMAC;
        public string StencilNum;
        public string Model;
        public string PartNumber;
        public string IGMPSnooping;
        public string DNSName;
        public string CodeVersion;
        public string VersionNumber;
        public string DHCPDisabled;
        public string DateInstalled;
        public string DateRemoved;
        public string Surplus;

        public override string ToString()
        {
            return RequestID;
        }
        public void Store()
        {
        }
        public void Remove()
        {
        }
        public void Update()
        {
        }
        public void Provision()
        {
        }
        public void Reboot()
        {
        }

    }
    public class AccountEntry
    {
        public AccountEntry(string requestID, string accountName, string accountAddress, string accountPremise, string primaryHubID)
        {
            RequestID = requestID;
            AccountName = accountName;
            AccountAddress = accountAddress;
            AccountPremise = accountPremise;
            PrimaryHubID = primaryHubID;
        }

        public string RequestID;
        public string AccountName;
        public string AccountAddress;
        public string AccountPremise;
        public string PrimaryHubID;

        public override string ToString()
        {
            return RequestID;
        }
    }
    public class HubEntry
    {
        private string _HubIPAddress;
        public HubEntry(string requestID, string hubName, string hubIPAddress, string scope, string facility, string hubLocation)
        {
            RequestID = requestID;
            HubName = hubName;
            _HubIPAddress = hubIPAddress;
            Scope = scope;
            Facility = facility;
            HubLocation = hubLocation;
        }
        public HubEntry(string remedyID, string hubName, IPAddress hubIPAddress, string scope, string facility, string hubLocation)
        {
            RequestID = remedyID;
            HubName = hubName;
            _HubIPAddress = hubIPAddress.ToString();
            Scope = scope;
            Facility = facility;
            HubLocation = hubLocation;
        }
        public int ThirdOctet
        {
            get
            {
                string[] values = Scope.Split('.');
                return int.Parse(values[2]);
            }
        }
        public string RequestID;
        public string Facility;
        public string HubLocation;
        public string Scope;
        public string HubName;

        public IPAddress HubIPAddress
        {
            get
            {
                IPAddress returnHubIP = null;
                IPAddress.TryParse(_HubIPAddress, out returnHubIP);
                return returnHubIP;
            }
        }

        public override string ToString()
        {
            return RequestID;
        }
    }
    public class VlanEntry
    {
        public VlanEntry(string requestID, string vlanNum, string vlanName, string isolated, string primaryVlan)
        {
            RequestID = requestID;
            VlanNum = vlanNum;
            VlanName = vlanName;
            Isolated = isolated;
            PrimaryVlan = primaryVlan;
        }

        public string RequestID;
        public string VlanNum;
        public string VlanName;
        public string Isolated;
        public string PrimaryVlan;

        public override string ToString()
        {
            return RequestID;
        }
    }
    public class SplicingEntry
    {
        public SplicingEntry(string requestID, string stencil, string switchPort, string switchBlade, string hubNumber, string switchIPAddress, string premise)
        {
            RequestID = requestID;
            Stencil = stencil;
            SwitchPort = switchPort;
            SwitchBlade = switchBlade;
            HubNumber = hubNumber;
            SwitchIPAddress = switchIPAddress;
            Premise = premise;
        }

        public string RequestID;
        public string Stencil;
        public string SwitchPort;
        public string SwitchBlade;
        public string HubNumber;
        public string SwitchIPAddress;
        public string Premise;

        public override string ToString()
        {
            return RequestID;
        }

    }
    public class GatewayAccountEntry
    {
        public GatewayAccountEntry(string requestID, string gatewayID, string accountID)
        {
            RequestID = requestID;

            GatewayID = gatewayID;
            AccountID = accountID;
        }
        public string RequestID;
        public string GatewayID;
        public string AccountID;
        public override string ToString()
        {
            return RequestID;
        }
    }
    public class QueueEntry
    {
        public QueueEntry(string requestID, string stencil, string gatewayIP, string command, string schedule, string result)
        {
            RequestID = requestID;

            Stencil = stencil;
            GatewayIP = gatewayIP;
            Command = command;
            Schedule = schedule;
            Result = result;
        }
        public string RequestID;
        public string Stencil;
        public string GatewayIP;
        public string Command;
        public string Schedule;
        public string Result;

        public override string ToString()
        {
            return RequestID;
        }
    }
    public class FieldLabel
    {
        public FieldLabel(Field val, uint vuiId)
        {
            val_ = val;
            vuiId_ = vuiId;
        }

        public string Name
        {
            get
            {
                return val_.Name;
            }
        }

        public string Label
        {
            get
            {
                if (val_.ExistsOnView(vuiId_))
                {
                    DisplayInstance di = (DisplayInstance)val_.DisplayInstanceList[(int)vuiId_];
                    if (val_.DataType == Constants.AR_DATA_TYPE_CONTROL)
                        return (string)di[Constants.AR_DPROP_BUTTON_TEXT];
                    else
                        return (string)di[Constants.AR_DPROP_LABEL];
                }
                else
                {
                    return "(not on view)";
                }
            }
            set
            {
                if (val_.ExistsOnView(vuiId_))
                {
                    DisplayInstance di = (DisplayInstance)val_.DisplayInstanceList[(int)vuiId_];
                    if (val_.DataType == Constants.AR_DATA_TYPE_CONTROL)
                        di[Constants.AR_DPROP_BUTTON_TEXT] = value;
                    else
                        di[Constants.AR_DPROP_LABEL] = value;
                }
            }
        }

        public Field GetField()
        {
            return val_;
        }

        private Field val_;
        private uint vuiId_;
    }


}
